﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;


namespace FinancialCalculator
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        Module.TimeValueOfMoney tv = new Module.TimeValueOfMoney();
        Boolean cpt_lock = false;
        Boolean displaying_finished = false;
        Boolean op_activited = false;
        //displaying_result is for detect whether user is continuing calculation after pressing "="
        Boolean displaying_result = false;
        String latest_op = "0";
        String op1 = "";
        String op2 = "";
        //if display is showing calculation result, anymore key pressed down should clear the display.

        public MainPage()
        {
            InitializeComponent();
            // Set the data context of the lisdisplay_infoox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }


        private void Number_Click(object sender, RoutedEventArgs e)
        {
            if (displaying_result)
            {
                op1 = "";
                op2 = "";
                displaying_result = false;
            }
            if (displaying_finished) { 
                display_result.Text = ""; 
                displaying_finished = false;
            }
            Button b = (Button)sender;
            //result show digits, info keep clear
            display_result.Text += b.Content.ToString();
            //Save the data
            if (!op_activited)
            {
                op1 += b.Content.ToString();
            }
            else
            {
                op2 += b.Content.ToString();
            }
        }

        private void Op_Click(object sender, RoutedEventArgs e)
        {
            if ((op1.Length == 0) && (op2.Length == 0))
            {
                op1 = "0";
                Result_click(sender, e);
            } 
            if ((op1.Length != 0) && (op2.Length != 0))
            {
                Result_click(sender, e);
            }
            Button b = (Button)sender;
            latest_op = b.Content.ToString();
            //result & info keep same
            displaying_finished = true;
            displaying_result = false;
            //op_activited now is true, all number input should be saved into op2
            op_activited = true;

        }

        private void Result_click(object sender, RoutedEventArgs e)
        {
            try
            {
                op_activited = false;
                //Clear display
                display_result.Text = "";
                display_info.Text = "";
                result(latest_op);
                displaying_finished = true;
                displaying_result = true;
            }
            catch (Exception exc)
            {
                display_info.Text = "Error!";
            }
        }
        
        private void result(String op)
        {
            double num1 = Convert.ToDouble(op1.ToString());
            double num2 = Convert.ToDouble(op2.ToString());

            if (op.Equals("+"))
            {
                display_info.Text += op1 + "+" + op2 + "=" + (num1 + num2);
                display_result.Text = (num1 + num2).ToString();
            }
            else if (op.Equals("-"))
            {
                display_info.Text += op1 + "-" + op2 + "=" + (num1 - num2);
                display_result.Text = (num1 - num2).ToString();
            }
            else if (op.Equals("*"))
            {
                display_info.Text += op1 + "*" + op2 + "=" + (num1 * num2);
                display_result.Text = (num1 * num2).ToString();
            }
            else if (op.Equals("/"))
            {
                display_info.Text += op1 + "/" + op2 + "=" + (num1 / num2);
                display_result.Text = (num1 / num2).ToString();
            }
            //save final result into op1, op2 clear
            op1 = display_result.Text;
            op2 = "";
            //save result to 1st op
        }

        private void R_Click(object sender, RoutedEventArgs e)
        {
            display_info.Text = "";
            display_result.Text = "";
            op1 = "";
            op2 = "";
            latest_op = "";
            tv.ResetTimeValueOfMoney();
            if (displaying_finished) { displaying_finished = false; }
            if (displaying_result) { displaying_result = false; }
            if (op_activited == true) { op_activited = false; }
        }

        private void Backspace_Click(object sender, RoutedEventArgs e)
        {
            if (display_result.Text.Length > 0)
            {
                display_result.Text = display_result.Text.Substring(0, display_result.Text.Length - 1);
            }
        }

        private void PV_Click(object sender, RoutedEventArgs e)
        {
            if (op_activited == true) op_activited = false;
            if (cpt_lock)
            {
                tv.setPV(tv.calculate_PV());
                display_result.Text = tv.getPV().ToString();
                display_info.Text = "PV = " + display_result.Text;
                cpt_lock = false;
            }
            else
            {
                tv.setPV(Convert.ToDouble(op1 == "" ? "0" : op1));
                display_info.Text = "PV Set Successfully!";
            }
            displaying_finished = true;
            displaying_result = true;
            op2 = "";
        }

        private void FV_Click(object sender, RoutedEventArgs e)
        {
            if (op_activited == true) op_activited = false;
            if (cpt_lock)
            {
                tv.setFV(tv.calculate_FV());
                display_result.Text = tv.getFV().ToString();
                display_info.Text = "FV = " + display_result.Text;
                cpt_lock = false;
            }
            else
            {
                tv.setFV(Convert.ToDouble(op1 == "" ? "0" : op1));
                display_info.Text = "FV Set Successfully!";
            }
            displaying_finished = true;
            displaying_result = true;
            op2 = "";
        }

        private void IY_Click(object sender, RoutedEventArgs e)
        {
            if (op_activited == true) op_activited = false;
            if (cpt_lock)
            {
                tv.setIY(tv.calculate_IY());
                display_result.Text = tv.getIY().ToString();
                display_info.Text = "I/Y = " + display_result.Text;
                cpt_lock = false;
            }
            else
            {
                tv.setIY(Convert.ToDouble(op1==""?"0":op1));
                display_info.Text = "Interest Set Successfully!";
            }
            displaying_finished = true;
            displaying_result = true;
            op2 = "";
        }

        private void N_Click(object sender, RoutedEventArgs e)
        {
            if (op_activited == true) op_activited = false;
            if ((op1.Length == 0) && (op2.Length == 0))
            {
                op1 = "0";
                Result_click(sender, e);
            }
            if ((op1.Length != 0) && (op2.Length != 0))
            {
                Result_click(sender, e);
            } 
            
            if (cpt_lock)
            {
                tv.setN(tv.calculate_n());
                display_result.Text = tv.getN().ToString();
                display_info.Text = "Period = " + display_result.Text;
                cpt_lock = false;
            }
            else
            {
                tv.setN((int)Convert.ToDouble(op1 == "" ? "0" : op1));
                display_info.Text = "n Set Successfully! - n = "+op1;

            }
            displaying_finished = true;
            displaying_result = true;
            op2 = "";
        }

        private void PMT_Click(object sender, RoutedEventArgs e)
        {
            if (op_activited == true) op_activited = false;
            if (cpt_lock)
            {
                tv.setPMT(tv.calculate_pmt());
                display_result.Text = tv.getPMT().ToString();
                display_info.Text = "PMT = " + display_result.Text;
                cpt_lock = false;
            }
            else
            {
                tv.setPMT(Convert.ToDouble(op1 == "" ? "0" : op1));
                display_info.Text = "PMT Set Successfully!";
            }
            displaying_finished = true;
            displaying_result = true;
            op2 = "";
        }

        private void CPT_Click(object sender, RoutedEventArgs e)
        {
            cpt_lock = true;
            display_info.Text = "Choose one item to compute...";
        }

    }
}